package leetcode.N201_N300;

import leetcode.comm.ListNode;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/13 7:29 下午
 */
public class N203 {

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        // 先找到一个有效的头
        while (head.val == val) {
            head = head.next;
            if (head == null) {
                return head;
            }
        }
        ListNode lastNode = head;
        ListNode current = head.next;
        while (current != null) {
            if (current.val == val) {
                lastNode.next = current = current.next;
            } else {
                lastNode = current;
                current = current.next;
            }
        }
        return head;
    }

}
